package com.eastone.user.serverapi.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eastone.user.serverapi.listener.SessionContext;
import com.eastone.user.serverapi.model.InfoUser;
import com.eastone.user.serverapi.service.InfoUserService;
import com.yjx.framework.cache.ServiceResultCodeCache;
import com.yjx.framework.constant.Constant;
import com.yjx.framework.model.ServiceResult;
import com.yjx.framework.request.YjxHttpServletRequest;
import com.yjx.framework.util.JsonUtil;
import com.yjx.framework.util.MathUtil;

@Controller
@RequestMapping(value = "/infoUser")
public class InfoUserController {

	private static final Logger LOG = LogManager.getLogger(InfoUserController.class);
	private static ServiceResultCodeCache resultCode = ServiceResultCodeCache.getInstance();
	@Autowired
	private InfoUserService infoUserService;

	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public @ResponseBody JSONObject login(YjxHttpServletRequest req) {
		String userId = req.getStr("userId");
		String password = req.getStr("password");
		if (LOG.isDebugEnabled()) {
			LOG.debug("get(YjxHttpServletRequest req)");
			LOG.debug("userId=" + userId);
			LOG.debug("password=" + password);
		}
		HttpSession session = req.getSession();
		JSONObject jsonObject = null;
		if (userId.equals("admin") && password.equals("lxfei")) {
			jsonObject = JsonUtil.getSuccessJsonObject();
			JSONObject data = new JSONObject();
			data.put("sessionId", session.getId());
			jsonObject.put("data", data);
			SessionContext.getInstance().getSessionMap().put(userId, session);
		} else {
			SessionContext.getInstance().DelSession(session);
			jsonObject = JsonUtil.getResultJsonObject(resultCode.get("common.username.password.error"), "用户名或密码错误");
		}
		return jsonObject;
	}

	@RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
	public @ResponseBody JSONObject get(YjxHttpServletRequest req, @PathVariable("id") int id) {
		if (LOG.isDebugEnabled()) {
			LOG.debug("get(YjxHttpServletRequest req)");
			LOG.debug("id=" + id);
		}

		InfoUser user = infoUserService.get(id);
		JSONObject jsonObject = JsonUtil.getSuccessJsonObject();
		jsonObject.put("data", JSONObject.toJSON(user));
		return jsonObject;
	}

	@RequestMapping(value = "/getList", method = RequestMethod.GET)
	public @ResponseBody JSONObject getList(YjxHttpServletRequest req) {
		String userName = req.getStr("userName");
		String userId = req.getStr("userId");
		String phoneNum = req.getStr("phoneNum");
		String email = req.getStr("email");
		String sex = req.getStr("sex");
		if (LOG.isDebugEnabled()) {
			LOG.debug("get(YjxHttpServletRequest req)");
			LOG.debug("userName=" + userName);
			LOG.debug("userId=" + userId);
			LOG.debug("phoneNum=" + phoneNum);
			LOG.debug("email=" + email);
			LOG.debug("sex=" + sex);
		}
		int pageNo = MathUtil.toDefaultInt(req.getInt("pageNo"), Constant.PAGE_NO_DEFAULT);
		int pageSize = MathUtil.toDefaultInt(req.getInt("pageSize"), Constant.PAGE_SIZE_DEFAULT);
		List<InfoUser> infoUsers = infoUserService.getList(userName, userId, phoneNum, email, sex, pageNo, pageSize);
		JSONObject jsonObject = JsonUtil.getSuccessJsonObject();
		jsonObject.put("data", JSONObject.toJSON(infoUsers));
		return jsonObject;
	}

	@RequestMapping(value = "/count", method = RequestMethod.GET)
	public @ResponseBody JSONObject count(YjxHttpServletRequest req) {
		String userName = req.getStr("userName");
		String userId = req.getStr("userId");
		String phoneNum = req.getStr("phoneNum");
		String email = req.getStr("email");
		String sex = req.getStr("sex");
		if (LOG.isDebugEnabled()) {
			LOG.debug("get(YjxHttpServletRequest req)");
			LOG.debug("userName=" + userName);
			LOG.debug("userId=" + userId);
			LOG.debug("phoneNum=" + phoneNum);
			LOG.debug("email=" + email);
			LOG.debug("sex=" + sex);
		}
		JSONObject jsonObject = JsonUtil.getSuccessJsonObject();
		jsonObject.put("data", infoUserService.getCount(userName, userId, phoneNum, email, sex));
		return jsonObject;
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public @ResponseBody JSONObject add(YjxHttpServletRequest req) {
		String userName = req.getStr("userName");
		String userId = req.getStr("userId");
		String phoneNum = req.getStr("phoneNum");
		String email = req.getStr("email");
		String sex = req.getStr("sex");
		String address = req.getStr("address");
		String password = req.getStr("password");
		if (LOG.isDebugEnabled()) {
			LOG.debug("get(YjxHttpServletRequest req)");
			LOG.debug("userName=" + userName);
			LOG.debug("userId=" + userId);
			LOG.debug("phoneNum=" + phoneNum);
			LOG.debug("email=" + email);
			LOG.debug("sex=" + sex);
		}

		InfoUser user = new InfoUser();
		user.setAddress(address);
		user.setEmail(email);
		user.setPassword(password);
		user.setPhoneNum(phoneNum);
		user.setSex(sex);
		user.setUserId(userId);
		user.setUserName(userName);
		ServiceResult<String> serviceResult = infoUserService.add(user);
		if (serviceResult.getCode().equals(resultCode.get("common.sucess"))) {
			return JsonUtil.getSuccessJsonObject();
		}
		return JsonUtil.getFailJsonObject();
	}

	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody JSONObject update(YjxHttpServletRequest req) {
		int id = req.getInt("id");
		String userName = req.getStr("userName");
		String userId = req.getStr("userId");
		String phoneNum = req.getStr("phoneNum");
		String email = req.getStr("email");
		String sex = req.getStr("sex");
		String address = req.getStr("address");
		String password = req.getStr("password");
		if (LOG.isDebugEnabled()) {
			LOG.debug("get(YjxHttpServletRequest req)");
			LOG.debug("userName=" + userName);
			LOG.debug("userId=" + userId);
			LOG.debug("phoneNum=" + phoneNum);
			LOG.debug("email=" + email);
			LOG.debug("sex=" + sex);
		}

		InfoUser user = new InfoUser();
		user.setId(id);
		user.setAddress("".equals(address) ? null : address);
		user.setEmail("".equals(email) ? null : email);
		user.setPassword("".equals(password) ? null : password);
		user.setPhoneNum("".equals(phoneNum) ? null : phoneNum);
		user.setSex("".equals(sex) ? null : sex);
		user.setUserId("".equals(userId) ? null : userId);
		user.setUserName("".equals(userName) ? null : userName);
		ServiceResult<String> serviceResult = infoUserService.updateSelective(user);
		if (serviceResult.getCode().equals(resultCode.get("common.sucess"))) {
			return JsonUtil.getSuccessJsonObject();
		}
		return JsonUtil.getFailJsonObject();
	}

	@RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
	public @ResponseBody JSONObject delete(YjxHttpServletRequest req, @PathVariable("id") int id) {
		if (LOG.isDebugEnabled()) {
			LOG.debug("id=" + id);
		}

		InfoUser user = new InfoUser();
		user.setId(id);
		ServiceResult<String> serviceResult = infoUserService.delete(user);
		if (serviceResult.getCode().equals(resultCode.get("common.sucess"))) {
			return JsonUtil.getSuccessJsonObject();
		}
		return JsonUtil.getFailJsonObject();
	}
	
	
	@RequestMapping(value = "/getListDataTables", method = RequestMethod.GET)
	public @ResponseBody JSONObject getListDataTables(YjxHttpServletRequest req) {
		
		String sEcho = req.getStr("sEcho");
		
		String userName = req.getStr("userName");
		String userId = req.getStr("userId");
		String phoneNum = req.getStr("phoneNum");
		String email = req.getStr("email");
		String sex = req.getStr("sex");
		if (LOG.isDebugEnabled()) {
			LOG.debug("getListDataTables(YjxHttpServletRequest req)");
			LOG.debug("userName=" + userName);
			LOG.debug("userId=" + userId);
			LOG.debug("phoneNum=" + phoneNum);
			LOG.debug("email=" + email);
			LOG.debug("sex=" + sex);
		}
		int pageNo = MathUtil.toDefaultInt(req.getInt("iDisplayStart")/10+1, Constant.PAGE_NO_DEFAULT);//pageNo
		int pageSize = MathUtil.toDefaultInt(req.getInt("iDisplayLength"), Constant.PAGE_SIZE_DEFAULT);
		List<InfoUser> infoUsers = infoUserService.getList(userName, userId, phoneNum, email, sex, pageNo, pageSize);
		
		int count = infoUserService.getCount(userName, userId, phoneNum, email, sex);
		JSONObject result = new JSONObject();
		result.put("aaData", infoUsers);
		result.put("iTotalRecords", count);
		result.put("sEcho", sEcho);
		result.put("iTotalDisplayRecords", count);
		return result;
	}
}
